Skip to content

Comments

cacheprovider: simplify cache directory creation#14121

Merged
RonnyPfannschmidt merged 3 commits intopytest-dev:mainfrom
RonnyPfannschmidt:cachedir-fill-files
Jan 29, 2026
Merged

cacheprovider: simplify cache directory creation#14121
RonnyPfannschmidt merged 3 commits intopytest-dev:mainfrom
RonnyPfannschmidt:cachedir-fill-files

Conversation

@RonnyPfannschmidt
Copy link
Member

Refactor _ensure_cache_dir_and_supporting_files to use a dedicated _make_cachedir function that atomically creates the cache directory with its supporting files (README.md, .gitignore, CACHEDIR.TAG).

  • Store all file contents as bytes in CACHEDIR_FILES dict
  • Use tempfile.mkdtemp + shutil.rmtree instead of TemporaryDirectory
  • Simplify cleanup logic for race conditions

Copilot AI review requested due to automatic review settings January 17, 2026 16:08
@RonnyPfannschmidt RonnyPfannschmidt added the skip news used on prs to opt out of the changelog requirement label Jan 17, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the cache directory creation logic in cacheprovider.py by extracting it into a dedicated _make_cachedir function, simplifying the code structure while maintaining atomic creation and proper race condition handling.

Changes:

  • Consolidated file contents (README.md, .gitignore, CACHEDIR.TAG) into a single CACHEDIR_FILES dictionary with bytes values
  • Extracted cache directory creation logic into a new _make_cachedir function
  • Replaced tempfile.TemporaryDirectory context manager with tempfile.mkdtemp + shutil.rmtree for simpler cleanup

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@RonnyPfannschmidt RonnyPfannschmidt force-pushed the cachedir-fill-files branch 2 times, most recently from b2505ff to 212c1e1 Compare January 17, 2026 22:49
Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, LGTM.

The except BaseException case is not covered, would be nice to add a test for it but OK if it's too hard.

RonnyPfannschmidt and others added 3 commits January 29, 2026 15:50
Refactor _ensure_cache_dir_and_supporting_files to use a dedicated
_make_cachedir function that atomically creates the cache directory
with its supporting files (README.md, .gitignore, CACHEDIR.TAG).

- Store all file contents as bytes in CACHEDIR_FILES dict
- Use tempfile.mkdtemp + shutil.rmtree instead of TemporaryDirectory
- Simplify cleanup logic for race conditions

Co-authored-by: Cursor AI <ai@cursor.sh>
Co-authored-by: Anthropic Claude Opus 4 <claude@anthropic.com>
Co-authored-by: Cursor AI <ai@cursor.sh>
Co-authored-by: Anthropic Claude Opus 4.5 <claude@anthropic.com>
Use a single finally block instead of duplicating cleanup logic in
separate except blocks. The finally block always runs, ensuring cleanup
whether the operation succeeds (rename moves dir away, rmtree is no-op),
fails with a race condition (ENOTEMPTY/EEXIST), or any other exception.

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
Co-authored-by: Cursor AI <ai@cursor.sh>
Co-authored-by: Anthropic Claude Opus 4.5 <claude@anthropic.com>
@RonnyPfannschmidt RonnyPfannschmidt merged commit dde81b1 into pytest-dev:main Jan 29, 2026
33 checks passed
@RonnyPfannschmidt RonnyPfannschmidt deleted the cachedir-fill-files branch January 29, 2026 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news used on prs to opt out of the changelog requirement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants